package team18.yuejian.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import team18.yuejian.Model.CommonModel;
import team18.yuejian.R;
import team18.yuejian.RetrofitListener;
import team18.yuejian.Server.CommonServer;
import team18.yuejian.bean.RegBean;

public class RegActivity extends AppCompatActivity implements View.OnClickListener,RetrofitListener<RegBean> {
    private Button Reg;
    private ImageButton back;
    private EditText unameView,uphoneView,upasswordView;
    private String sexText;
    private int ugender;


    private RadioGroup group;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        InitView();
        InitEvent();
    }


    public void selectRadioButton(){
        group = findViewById(R.id.reg_choose_sex);
        RadioButton rb = RegActivity.this.findViewById(group.getCheckedRadioButtonId());
        sexText = rb.getText().toString();
        Log.d("Rediobutton",rb.getText().toString());
    }

    public  void InitView(){
        unameView = findViewById(R.id.reg_username);
        upasswordView = findViewById(R.id.reg_password);
        uphoneView = findViewById(R.id.reg_tel);
        group = findViewById(R.id.reg_choose_sex);
        Reg = findViewById(R.id.reg_bt);
        back = findViewById(R.id.reg_back);
    }

    public void InitEvent(){
        Reg.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.reg_bt:
                click(0); break;

            case  R.id.reg_back:
                click(1); break;
        }

    }

    public void click(int num){
        if (num==0){
            selectRadioButton();
            if ("男".equals(sexText)){
                ugender = 1;
            }else{
                ugender = 0;
            }
            CommonModel commonModel = new CommonModel();
            commonModel.UserReg(unameView.getText().toString().trim(),uphoneView.getText().toString().trim(),
                    upasswordView.getText().toString().trim(),ugender,this);
        }else if (num==1){
            Intent intent = new Intent(RegActivity.this,LoginActivity.class);
            Toast.makeText(RegActivity.this,"返回到登录界面",Toast.LENGTH_SHORT).show();
            startActivity(intent);
        }
    }

    @Override
    public void onSuccess(RegBean regBean, int flag) {
        if ("0".equals(regBean.getError())){
            Toast.makeText(RegActivity.this,"用户名已被注册",Toast.LENGTH_SHORT).show();
        }else if("1".equals(regBean.getError())){
            Toast.makeText(RegActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(RegActivity.this,"注册成功返回登录",Toast.LENGTH_SHORT);
            Intent intent = new Intent();
            intent.putExtra("username",unameView.getText().toString());
            intent.putExtra("password",upasswordView.getText().toString());

            setResult(RESULT_OK,intent);
            finish();
        }
    }

    @Override
    public void onFail(String msg) {

    }
}
